import GxSingleCamMono
# pillow is used to show acquired image
from PIL import Image
import common
from gxipy.gxidef import *
import numpy
from gxipy.ImageProc import Utility
import gxipy as gx
import jyzk_io as io

class GxCamera:

    device_manager = None
    cam = None
    dev_num = 0
    dev_info_list = None
    remote_device_feature = None
    num = 1

    def __init__(self):
        self.device_manager = gx.DeviceManager()

    def open_camera(self):
        self.dev_num, self.dev_info_list = self.device_manager.update_all_device_list()
        if self.dev_num == 0:
            print("Number of enumerated devices is 0")
            return None

        # open the first device
        self.cam = self.device_manager.open_device_by_index(1)
        self.remote_device_feature = self.cam.get_remote_device_feature_control()

        # get image convert obj
        global image_convert
        image_convert = self.device_manager.create_image_format_convert()

        # exit when the camera is a color camera
        pixel_format_value, pixel_format_str = self.remote_device_feature.get_enum_feature("PixelFormat").get()
        if Utility.is_gray(pixel_format_value) is False:
            print("This sample does not support color camera.")
            self.cam.close_device()
            return
        return self.cam

    def close_camera(self):
        self.cam.close_device()

    def get_image(self):
        # start data acquisition
        self.cam.stream_on()

        # acquire image: num is the image number
        numpy_images = []
        save_file_names = []
        for i in range(self.num):
            # get raw image
            raw_image = self.cam.data_stream[0].get_image()
            if raw_image is None:
                #print("Getting image failed.")
                continue

            if raw_image.get_pixel_format() not in (
                    GxPixelFormatEntry.MONO8, GxPixelFormatEntry.R8, GxPixelFormatEntry.B8, GxPixelFormatEntry.G8):
                mono_image_array, mono_image_buffer_length = GxSingleCamMono.convert_to_special_pixel_format(raw_image,
                                                                                             GxPixelFormatEntry.MONO8)
                if mono_image_array is None:
                    return
                # create numpy array with data from rgb image
                numpy_image = numpy.frombuffer(mono_image_array, dtype=numpy.ubyte, count=mono_image_buffer_length). \
                    reshape(raw_image.frame_data.height, raw_image.frame_data.width)
            else:
                numpy_image = raw_image.get_numpy_array()

            if numpy_image is None:
                continue
            else:
                numpy_images.append(numpy_image)

            # show acquired image
            # img = Image.fromarray(numpy_image, 'L')
            # img.show()

            qimage = io.numpy_to_qimage(numpy_image)
            # show acquired image
            # qimage.show()

            # print height, width, and frame ID of the acquisition image
            print("Frame ID: %d   Height: %d   Width: %d"
                  % (raw_image.get_frame_id(), raw_image.get_height(), raw_image.get_width()))

            # # 保存图片
            # save_path = common.get_current_path() + "Pictures/"
            # save_file_name = save_path + "image_" + common.get_current_time() + "_" + i.__str__() + ".jpg"
            # img.save(save_file_name)
            # save_file_names.append(save_file_name)

        # stop data acquisition
        self.cam.stream_off()
        return qimage